package com.twu.biblioteca;

import java.util.Objects;

class Movie {

    private final String title;

    private final int year;

    private final String director;

    private Integer rating;

    Movie(String title, int year, String director, Integer rating) {
        this.title = title;
        this.year = year;
        this.director = director;
        setRating(rating);
    }

    private void setRating(Integer rating) {
        if (rating != null) {
            if (rating < 1 || rating > 10) {
                throw new IllegalArgumentException();
            }
        }
        this.rating = rating;
    }

    String getTitle() {
        return title;
    }

    int getYear() {
        return year;
    }

    String getDirector() {
        return director;
    }

    @Override
    public int hashCode() {
        return Objects.hash(title, year, director);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || !(obj instanceof Movie)) {
            return super.equals(obj);
        }
        Movie other = (Movie) obj;
        return other.title.equals(title) && other.year == year && other.director.equals(director);
    }

    public String getRatingStringRepresentation() {
        final String result;
        if (rating == null) {
            result = "N/A";
        } else {
            result = rating.toString();
        }
        return result;
    }
}
